﻿<Serializable()> _
Public Class clsNotificationsManager
	Inherits clsBaseClass

	Private notifications As clsNotificationCollection
	Private WithEvents tmrObserver As System.Windows.Forms.Timer

	Public Event FileLoaded As EventHandler
	Public Event FileSaved As EventHandler

	Private strCurFilePath As String

	Public Property CurrentFilePath() As String
		Get
			Return strCurFilePath
		End Get
		Set(ByVal value As String)
			strCurFilePath = value
		End Set
	End Property

	Public ReadOnly Property NotificationItems() As clsNotificationCollection
		Get
			Return notifications
		End Get
	End Property

	Public Sub New()
		Init(Nothing)
	End Sub

	Public Sub New(ByVal tmr As System.Windows.Forms.Timer)
		Init(tmr)
	End Sub

	Public Sub New(ByVal strName As String, ByVal tmr As System.Windows.Forms.Timer)
		MyBase.New(strName)
		Init(tmr)
	End Sub

	Private Sub Init(ByVal tmr As System.Windows.Forms.Timer)
		SetTimer(tmr)
		notifications = New clsNotificationCollection
		Name = My.Application.Info.ProductName
	End Sub

	Public Sub SetTimer(ByVal tmr As System.Windows.Forms.Timer)
		tmrObserver = tmr
	End Sub

	Private Sub tmrObserver_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrObserver.Tick
		For Each item As clsNotification In notifications.Items
			item.Notify()
		Next
	End Sub

	Public Function AddNewItem() As clsNotification
		Return AddItem(New clsNotification("New Item " & (notifications.Items.Count + 1)))
	End Function

	Public Function AddItem(ByVal item As clsNotification)
		notifications.Items.Add(item)
		Return item
	End Function

	Protected Overrides Sub Finalize()
		Dispose()
		MyBase.Finalize()
	End Sub

	Public Overrides Sub Dispose()
		If notifications IsNot Nothing Then
			notifications.Dispose()
			notifications = Nothing
		End If
		If tmrObserver IsNot Nothing Then
			tmrObserver.Enabled = False
			tmrObserver.Dispose()
			tmrObserver = Nothing
		End If
		MyBase.Dispose()
	End Sub

	Public Function SaveToFile(ByVal strFilePath As String) As Boolean
		Try
			Dim s As New Xml.Serialization.XmlSerializer(notifications.GetType())
			Using w As New Xml.XmlTextWriter(strFilePath, System.Text.Encoding.UTF8)
				s.Serialize(w, notifications)
				w.Flush()
				w.Close()
			End Using
			strCurFilePath = strFilePath
			RaiseEvent FileSaved(Me, EventArgs.Empty)
			Return True
		Catch ex As Exception
			MessageBox.Show(ex.Message.Trim(), "Error(s) while writing file", MessageBoxButtons.OK, MessageBoxIcon.Error)
			RegisterException(ex)
		End Try
		Return False
	End Function

	Public Function LoadFromFile(ByVal strFilePath As String) As Boolean
		Try
			Dim s As New Xml.Serialization.XmlSerializer(notifications.GetType())
			Dim obj As clsNotificationCollection
			Using r As New Xml.XmlTextReader(strFilePath)
				obj = s.Deserialize(r)
				r.Close()
			End Using
			If obj Is Nothing Then Exit Try
			notifications.Items.Clear()
			notifications.Items.AddRange(obj.Items)
			notifications.Name = obj.Name
			notifications.ID = obj.ID
			notifications.Enabled = obj.Enabled
			strCurFilePath = strFilePath
			RaiseEvent FileLoaded(Me, EventArgs.Empty)
			Return True
		Catch ex As Exception
			MessageBox.Show(ex.Message.Trim(), "Error(s) while writing file", MessageBoxButtons.OK, MessageBoxIcon.Error)
			RegisterException(ex)
		End Try
		Return False
	End Function
End Class
